import requests

from requests.exceptions import Timeout

from db_method import DataBaseMethod

class SettlementCallback(DataBaseMethod): # 结算回调类

    def __init__(self) -> None:

        super().__init__()

        self.callback_url = self.get_callback_url()

        if not self.callback_url: # 如果没有配置回调连接则终段回调执行

            return
        
        self.callback_task_list = self.get_callback_task_list() # 回调任务列表


        for door_task in self.callback_task_list:

            self.callback_function(door_task)

    
    def get_callback_url(self)-> str:

        '''获取回调链接'''

        settlement_callback_info = self.get_settlement_callback_info()

        if not settlement_callback_info:

            return None
        
        else:

            return settlement_callback_info["callback_url"]
        

    def get_callback_task_list(self) -> list: # 获取要回调的列表

        '''return: 返回一个door_task任务的id列表'''

        door_task_list = self.get_door_task_by_sync_status_not_success()


        return door_task_list
    

    def callback_function(self, door_task): # 向回调链接发送同步数据

        data = {

            "door_task_id": door_task["id"],

            "orderid": door_task["orderid"],

            "settlement": door_task["settlement"]

        }

        self.settlement_callback_function(**data)


if __name__ == "__main__":

    # 结算回调巡检

    SettlementCallback()




        



    


